//
//  JSReaderBook.m
//  NiubilityProject
//
//  Created by Apple on 2018/5/24.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "JSReaderBook.h"
#import "JSReaderChapter.h"

@implementation JSReaderBook
- (BOOL)haveNextChapter{
    return _totalChapter > _curChpaterIndex;
}

- (BOOL)havePreChapter{
    return _curChpaterIndex > 1;
}

- (void)resetChapter:(JSReaderChapter *)chapter{
    _curChpaterIndex = chapter.chapterIndex;
}

- (JSReaderChapter *)openBookWithChapter:(NSInteger)chapterIndex{
    JSReaderChapter *readerChapter = [[JSReaderChapter alloc]init];
    readerChapter.chapterIndex = chapterIndex;
    _curChpaterIndex = chapterIndex;
    NSError *error = nil;
    NSString *chapter_num = [NSString stringWithFormat:@"Chapter%d",(int)chapterIndex];
    NSString *path1 = [[NSBundle mainBundle] pathForResource:chapter_num ofType:@"txt"];
    readerChapter.chapterContent = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:&error];
    
    if (error) {
        NSLog(@"open book chapter error:%@",error);
        return nil;
    }
    return readerChapter;
}

- (JSReaderChapter *)openBookNextChapter{
    return [self openBookWithChapter:_curChpaterIndex+1];
}

- (JSReaderChapter *)openBookPreChapter{
    return [self openBookWithChapter:_curChpaterIndex-1];
}
@end
